package com.sh.sales.happysales.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.sh.polymedicine.happysale.result.SimpleResult;
import com.sh.polymedicine.happysale.result.UserInfoResult;
import com.sh.polymedicine.happysale.service.envelop.HappySaleManager;
import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.AppData;
import com.sh.sales.happysales.app.utils.LoginOutUtil;
import com.sh.sales.happysales.base.BaseActivity;
import com.sh.sales.happysales.imgutil.LoadImageManager;
import com.sh.sales.happysales.service.LoginObsever;
import com.sh.sales.happysales.view.NewToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by zhouyucheng on 2017/9/21.
 */

public class MyZingActivity extends BaseActivity implements LoginObsever {
    @BindView(R.id.set_back)
    ImageView setBack;
    @BindView(R.id.profect_next)
    TextView profectNext;
    @BindView(R.id.zing_ing1)
    ImageView zingIng1;
    @BindView(R.id.zing_nick)
    TextView zingNick;
    @BindView(R.id.zing_num)
    TextView zingNum;
    @BindView(R.id.zing_ing2)
    ImageView zingIng2;
    private DisplayImageOptions options;


    private Subscriber<SimpleResult> myZingSubscriber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myzing);
        ButterKnife.bind(this);
        mAppManager.addActivity(this);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.bg_start_page)
                .showImageForEmptyUri(R.drawable.bg_start_page)
                .displayer(new RoundedBitmapDisplayer(4))
                .showImageOnFail(R.drawable.bg_start_page)
                .imageScaleType(ImageScaleType.EXACTLY)
                .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    private void init() {
        UserInfoResult result = AppData.getInstence().getmUserInfoResult();
        mLoadImageManager.displayImage(result.getPortrait(), zingIng1, LoadImageManager.IMG, options);
        zingNick.setText(result.getNickname());
        zingNum.setText(result.getMobile());

        if (myZingSubscriber != null) {
            if (myZingSubscriber.isUnsubscribed()) {
                myZingSubscriber.unsubscribe();
            }
        }
        myZingSubscriber = new Subscriber<SimpleResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i("MyZingActivity", "onError: " + e);
                if (HappySaleManager.DATA_FAIL.equals(e.getMessage())) {
                    String message = e.getCause().getCause().getMessage();
                    NewToast.makeText(MyZingActivity.this, message);
                    int ret =Integer.valueOf( e.getCause().getMessage()) ;
                    if (ret>=100&&ret<200){
                        LoginOutUtil.logOut(MyZingActivity.this);
                    }
                } else if (HappySaleManager.DATA_NULL.equals(e.getMessage())) {
                    NewToast.makeText(MyZingActivity.this, e.getCause().getMessage());
                } else {
                    NewToast.makeText(MyZingActivity.this, "获取二维码失败，请检查网络");
                }
            }

            @Override
            public void onNext(SimpleResult result) {
                mLoadImageManager.displayImage(result.getImage(), zingIng2, LoadImageManager.IMG, options);
            }
        };
        mHappySaleManager.makePortrait(AppData.getInstence().getmUserToken().getUserSign(), myZingSubscriber).subscribe(myZingSubscriber);
    }


    @OnClick(R.id.set_back)
    public void onViewClicked() {
        mAppManager.finishActivity(this);
    }

    @Override
    public void refreshUserSign(int Position) {
        init();
    }

    @Override
    protected void onResume() {
        init();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mLoginObserable.unregisterObserver(this);
        super.onDestroy();
    }
}
